package com.puman.watchtrade.fragment.info.adpter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.puman.watchtrade.MainActivity;
import com.puman.watchtrade.R;
import com.puman.watchtrade.fragment.home.imageLoader.util.CheckImageLoaderConfiguration;
import com.puman.watchtrade.fragment.home.imageLoader.util.UniversalImageLoadTool;
import com.puman.watchtrade.fragment.info.model.InfoDatas;
import com.puman.watchtrade.util.ImageDownloader;
import com.puman.watchtrade.util.OnImageDownload;

public class InfoListViewAdapter extends BaseAdapter{

	private LayoutInflater listContainer; // ��ͼ����
	private View view;
	private Activity activity;
	private ImageDownloader mDownloader;
	private InfoDatas infoDatas = new InfoDatas();

	public final class ListItemView {
		public TextView title;
		public TextView detail;
		public ImageView image;
	}

	public InfoListViewAdapter(Activity activity, InfoDatas infoDatas) {
		this.activity = activity;
		this.infoDatas = infoDatas;
		listContainer = LayoutInflater.from(activity);
		// TODO Auto-generated constructor stub
	}

	
	/**
	 */
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ListItemView listItemView;
		final int selectID = position;
		if (convertView == null) {
			listItemView = new ListItemView();
			convertView = listContainer.inflate(R.layout.info_item, null);
			listItemView.title = (TextView) convertView.findViewById(R.id.info_title);
			listItemView.detail = (TextView) convertView.findViewById(R.id.info_detail);
			listItemView.image = (ImageView) convertView.findViewById(R.id.info_item_image);
			convertView.setTag(listItemView);
		} else {
			listItemView = (ListItemView) convertView.getTag();

		}
		if (infoDatas.infoList.get(position).getTitle().length() > 20) {
			listItemView.title.setText(infoDatas.infoList.get(position).getTitle().substring(0, 20)+"...");
		}else {
			listItemView.title.setText(infoDatas.infoList.get(position).getTitle());
		}
		if (infoDatas.infoList.get(position).getDes().length() > 23) {
			listItemView.detail.setText(infoDatas.infoList.get(position).getDes().substring(0, 23)+"...");
		}else {
			listItemView.detail.setText(infoDatas.infoList.get(position).getDes());
		}
		
//		if (!infoDatas.infoList.get(position).getPic().equals("")) {
//
//			String url = "";
//			url = infoDatas.infoList.get(position).getPic();
//			url = url.replace(" ", "");
//			Log.i("curry", "url:"+url);
//			listItemView.image.setTag(infoDatas.infoList.get(position).getPic());
//			if (mDownloader == null) {
//				mDownloader = new ImageDownloader();
//			}
//			if (mDownloader != null) {
//				mDownloader.imageDownload(url, listItemView.image,
//						"/watchtrade/image", activity, new OnImageDownload() {
//							@Override
//							public void onDownloadSucc(Bitmap bitmap,
//									String c_url, ImageView mimageView) {
//								ImageView imageView = (ImageView) listItemView.image
//										.findViewWithTag(c_url);
//								Drawable drawable =new BitmapDrawable(bitmap);
//								if (imageView != null) {
//									imageView.setBackgroundDrawable(drawable);
//									imageView.setTag("");
//								}
//							}
//						});
//			}
//		}
		
		CheckImageLoaderConfiguration.checkImageLoaderConfiguration(MainActivity.getInstant());
		
		String url = infoDatas.infoList.get(position).getPic().replace(" ", "");
//		Log.i("curry", "url:"+url);
		UniversalImageLoadTool.display(url, (ImageView)listItemView.image, R.drawable.icon);

		return convertView;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return infoDatas.infoList.size();
	}

	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public long getItemId(int arg0) {
		// TODO Auto-generated method stub

		return 0;
	}

}